import TaskController from "@/contrib/task/controller"
import { TaskPriority } from "@/contrib/task/types"
import LoopObject from "@/loop"
import { useCache } from "@/utils"
import powerSpawn from "../global/powerSpawn"
import mountShortcut from "../shortcut"
import { defendMyRoom } from "../tower"
import RoomController from "./controller"

export default class RoomControlLoopObject extends LoopObject {

    tickStart(): void {
    }
    do(): void {

        mountShortcut()

        if (Game.time % 2 == 0) {
            TaskController.cleanTask()
        }


        Object.values(Game.rooms).forEach(room => {

            if (!room.memory || !room.memory.source) {

                const sources = {}
                if (room['source']) {
                    let sourceIndex = 0
                    room['source'].forEach(source => {
                        sources[source.id] = {
                            index: sourceIndex++
                            // dropped: '29,20',
                            // containerId: ''
                            // linkId: ''
                        }
                    })
                }

                const managers: string[] = []
                const workers: string[] = []

                room.memory = {
                    // constructionSiteId: '07ed1ebc553f6e3',
                    source: sources,
                    managers: managers,
                    workers: workers,
                    work: {
                        tasks: [],
                        creeps: {}
                    },
                    lab: {
                        boostTasks: [],
                        boostingNote: {}
                    },
                    centerTasks: []
                }
            }

            // 外矿II
            if (room.name == 'E34S48') {

            }

            // 外矿I
            if (room.name == 'E35S48') {

                //
                if (Game.time % 5) return

                const homeRoom = Game.rooms['E35S49']
                const storage = homeRoom[STRUCTURE_STORAGE]

                const containers = room[STRUCTURE_CONTAINER]
                Object.values(containers).forEach(container => {
                    if (container && container.store.getUsedCapacity() >= 800) {
                        TaskController.addTransportTask(
                            TaskPriority.HIGH, room.name, homeRoom.name, container.id, storage.id, RESOURCE_ENERGY, 0
                        )
                    }
                })
            }

            if (!room.controller || !room.controller.owner || !(room.controller.owner.username == 'noahw')) return

            defendMyRoom.run(room)

            // Room Stats
            RoomController.showStats(room)

            // 房间IV
            if (room.name == 'E35S48') {

                //
                if (Game.time % 2) return

                const spawn = room[STRUCTURE_SPAWN][0]
                if (spawn.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
                    // COLLECT container
                    const container = spawn.pos.findClosestByPath(FIND_STRUCTURES, {
                        filter: s => s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] >= 100
                    })
                    if (container) {
                        TaskController.addTransportTask(
                            TaskPriority.HIGH, room.name, room.name, container.id, spawn.id, RESOURCE_ENERGY, 0
                        )
                    }
                }
            }

            // 房间III
            if (room.name == 'E45S48') {

                //
                if (Game.time % 2) return

                const storage = room[STRUCTURE_STORAGE]
                const droppedEnergys = room.find(FIND_DROPPED_RESOURCES)
                Object.values(droppedEnergys).forEach(droppedEnergy => {
                    if (droppedEnergy.amount >= 100) {
                        TaskController.addTransportTask(
                            TaskPriority.LOW, room.name, room.name, droppedEnergy.id, storage.id, RESOURCE_ENERGY, 0
                        )
                    }
                })
            }

            // 房间II
            if (room.name == 'E39S52') {

                // if (Game.time % 2) return

                // const storage = room[STRUCTURE_STORAGE]

                // if (!storage) {
                //     return null
                // }

                // const terminal: StructureTerminal = room[STRUCTURE_TERMINAL] as StructureTerminal
                // if (terminal) {
                //     // 100k more => PUT terminal 10k
                //     if (storage.store[RESOURCE_ENERGY] >= 30000) {
                //         room.memory.isTerminalSending = true
                //         TaskController.addTransportTask(
                //             TaskPriority.LOW, room.name, room.name, storage.id, terminal.id, RESOURCE_ENERGY, 1000
                //         )
                //     }
                //     else {
                //         // send 100k to HomeRoom
                //         if (terminal.store[RESOURCE_ENERGY] >= 34000) {
                //             const status = terminal.send(RESOURCE_ENERGY, 30000, 'E35S49')
                //             console.log('send 100k to HomeRoom', status)
                //             room.memory.isTerminalSending = false
                //         }
                //     }
                // }
            }

            // 房间I
            if (room.name == 'E35S49') {
                //
                if (Game.time % 2) return

                const storage = room[STRUCTURE_STORAGE]

                if (!storage) {
                    return null
                }

                const powerSpawn = room[STRUCTURE_POWER_SPAWN]
                if (storage.store[RESOURCE_ENERGY] > 600000) {
                    if (powerSpawn && (powerSpawn.store.getFreeCapacity(RESOURCE_ENERGY) > 2500 || powerSpawn.store.getFreeCapacity(RESOURCE_POWER) > 50)) {
                        if (powerSpawn.store.getFreeCapacity(RESOURCE_ENERGY) > 2500) {
                            if (storage.store[RESOURCE_ENERGY] > 0) {
                                TaskController.addTransportTask(
                                    TaskPriority.LOW, room.name, room.name, storage.id, powerSpawn.id, RESOURCE_ENERGY, powerSpawn.store.getFreeCapacity(RESOURCE_ENERGY)
                                )
                            }
                        }
                        if (powerSpawn.store.getFreeCapacity(RESOURCE_POWER) > 50) {
                            if (storage.store[RESOURCE_POWER] > 0) {
                                TaskController.addTransportTask(
                                    TaskPriority.LOW, room.name, room.name, storage.id, powerSpawn.id, RESOURCE_POWER, powerSpawn.store.getFreeCapacity(RESOURCE_POWER)
                                )
                            }
                        }
                    }
                }

                if (powerSpawn && (powerSpawn.store[RESOURCE_ENERGY] >= 50 && powerSpawn.store[RESOURCE_POWER] >= 1)) {
                    powerSpawn.processPower()
                }

                // pickup dropped resource
                const droppedEnergys = room.find(FIND_DROPPED_RESOURCES)
                Object.values(droppedEnergys).forEach(droppedEnergy => {
                    if (droppedEnergy.amount >= 100) {
                        TaskController.addTransportTask(
                            TaskPriority.LOW, room.name, room.name, droppedEnergy.id, storage.id, RESOURCE_ENERGY, 0
                        )
                    }
                })
            }

            // 房间通用
            if (room.name == 'E35S49' || room.name == 'E39S52' || room.name == 'E45S48') {

                //
                if (Game.time % 2) return

                const storage = room[STRUCTURE_STORAGE]

                if (!storage) {
                    return null
                }

                // COLLECT container
                const containers = room[STRUCTURE_CONTAINER].filter(s => s.store[RESOURCE_ENERGY] >= 400)
                Object.values(containers).forEach(container => {
                    if (TaskController.getTransportTaskByFromId(container.id)) {
                        console.log('TSK EXT--', container.id)
                    }
                    else {
                        TaskController.addTransportTask(
                            TaskPriority.HIGH, room.name, room.name, container.id, storage.id, RESOURCE_ENERGY, 0
                        )
                    }
                })

                // COLLECT terminal
                // const terminal = room[STRUCTURE_TERMINAL]
                // if (terminal && terminal.store[RESOURCE_ENERGY] > 10000) {
                //     TaskController.addTransportTask(
                //         TaskPriority.LOW, room.name, room.name, terminal.id, storage.id, RESOURCE_ENERGY, 1000
                //     )
                // }

                //
                // COLLECT droppedEnergy
                // const droppedEnergyTargets = room.source
                //     .map(source => source.getDroppedInfo().energy.amount >= 100)
                // Object.values(droppedEnergyTargets).forEach(droppedEnergyTarget => {
                //     TaskController.addTransportTask(
                //         room.name, room.name, droppedEnergyTarget, storage.id, RESOURCE_ENERGY, 0
                //     )
                // })

                // TRANSFER GH
                if (room[STRUCTURE_LAB]) {
                    const labs = room[STRUCTURE_LAB].filter(s =>
                        s.store.getFreeCapacity(RESOURCE_ENERGY) > 0 || s.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) || s.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0
                    )
                    Object.values(labs).forEach(lab => {
                        if (lab.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && storage.store[RESOURCE_ENERGY] > 0) {
                            TaskController.addTransportTask(
                                TaskPriority.LOW, room.name, room.name, storage.id, lab.id, RESOURCE_ENERGY, lab.store.getFreeCapacity(RESOURCE_ENERGY)
                            )
                        }
                        else if (lab.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) > 0 && storage.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                            TaskController.addTransportTask(
                                TaskPriority.LOW, room.name, room.name, storage.id, lab.id, RESOURCE_GHODIUM_HYDRIDE, lab.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE)
                            )
                        }
                        else if (lab.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0 && storage.store[RESOURCE_GHODIUM_ACID] > 0) {
                            TaskController.addTransportTask(
                                TaskPriority.LOW, room.name, room.name, storage.id, lab.id, RESOURCE_GHODIUM_ACID, lab.store.getFreeCapacity(RESOURCE_GHODIUM_ACID)
                            )
                        }
                    })
                }

                if (room[STRUCTURE_TERMINAL]) {
                    const terminal = room[STRUCTURE_TERMINAL]
                    const labs = room[STRUCTURE_LAB].filter(s =>
                        s.store.getFreeCapacity(RESOURCE_ENERGY) > 0 || s.store.getFreeCapacity(RESOURCE_GHODIUM_HYDRIDE) || s.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0
                    )
                    Object.values(labs).forEach(lab => {
                        if (lab.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 0 && terminal.store[RESOURCE_GHODIUM_ACID] > 0) {
                            TaskController.addTransportTask(
                                TaskPriority.LOW, room.name, room.name, terminal.id, lab.id, RESOURCE_GHODIUM_ACID, lab.store.getFreeCapacity(RESOURCE_GHODIUM_ACID)
                            )
                        }
                    })
                }
            }

        })

    }
    afterWork(): void {
    }
    tickEnd(): void {
    }
}
